<?php

namespace app\crontab;

use app\task\CloudTask;
use zhanshop\App;
use zhanshop\Crontab;

class CloudLogStatCrontab extends Crontab
{

    public function configure()
    {
        $this->setTitle("每天1点执行一次")->everyday(1);
    }

    public function execute()
    {
        App::task(CloudTask::class)->nginxStats(App::rootPath().'/docker/nginx/logs/access-'.date('Ymd', strtotime('-1 day')).'.log');

        $logExpireDays = App::env()->get('LOG_EXPIRE_DAYS', "15");
        // 清理容器stats多余的日志
        $files = glob(App::runtimePath().'/cloud/stats/*.log');
        $lastDate = intval(date('Ymd', strtotime('-'.$logExpireDays.' day')));
        foreach ($files as $file){
            $arr = explode('-', $file);
            if(isset($arr[2]) && intval($arr[2]) < $lastDate){
                @unlink($file);
            }
        }

        // 清理运维多余的日志
        $files = glob(App::runtimePath().'/devops/*/*.log');
        foreach ($files as $file){
            $paths = explode('/', explode('.log', $file)[0]);
            $fileName = intval($paths[count($paths) - 1]);
            if($fileName < $lastDate){
                @unlink($file);
            }
        }

    }
}